const http = require('../../utils/http.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetails: '',
    allPic: [],
    order_id: '',
    reasonShow: false,
    reasonArr: [],
    reason_id: '',
    status: ''
  },

  onLoad: function(options) {
    let order_id = options.order_id
    let status = options.status
    this.setData({
      status: status
    })
    //获取订单详情
    http.getData("order/" + order_id).then(res => {
      // console.log(res)
      this.setData({
        orderDetails: res,
        allPic: res.pictures.split(','),
        order_id: order_id
      })
    }).catch(res => {
      console.log(res)
    })

    //获取取消理由
    http.getData("reason").then(res => {
      // console.log(res)
      this.setData({
        reasonArr: res
      })
    }).catch(res => {
      console.log(res)
    })
  },

  cancelOrder() {
    this.setData({
      reasonShow: true
    })
  },

  //取消订单原因选择
  radioChange(e) {
    // console.log(e)
    let reason_id = e.detail.value;
    this.setData({
      reason_id: reason_id
    })
  },
  //确认
  choiceConfirm() {
    if (this.data.reason_id == '') {
      wx.showToast({
        title: '请选择取消原因',
        icon: 'none',
        image: '/images/tanhao.png'
      })
    } else {
      // console.log(this.data.reason_id)
      wx.request({
        url: 'https://api.mokyun.com/pesticide/del/order',
        method: 'POST',
        header: {
          'Accept': 'application/json',
          'Authorization': 'Bearer ' + wx.getStorageSync('token'),
          'content-type': 'application/json',
          'APPID': 32
        },
        data: {
          id: this.data.order_id,
          reason_id: this.data.reason_id
        },
        success: (res) => {
          // console.log(res)
          if (res.data == 'success') {
            wx.showToast({
              title: '取消成功',
              success: (res) => {
                setTimeout(() => {
                  // wx.redirectTo({
                  //   url: '/pages/order/order',
                  // })
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2500)
              }
            })
          }
        }
      })
    }
  },
  //取消
  choiceCancel() {
    this.setData({
      reasonShow: false
    })
  },
  goPay(e) {
    // console.log(e)
    let tradeNo = e.currentTarget.dataset.tradeno
    wx.request({
      url: 'https://api.mokyun.com/wechat/pay',
      method: 'POST',
      data: {
        tradeNo: tradeNo
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token'),
        'APPID': 32
      },
      success: function(res) {
        // console.log(res)
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success: function(res) {
            wx.request({
              url: "https://api.mokyun.com/pesticide/order/status",
              method: 'POST',
              data: {
                tradeNo: tradeNo
              },
              header: {
                'Authorization': 'Bearer ' + wx.getStorageSync('token'),
                'APPID': 32
              },
              success: function(res) {
                // console.log(res)
                if (res.data == 'success') {
                  wx.showToast({
                    title: '发布成功',
                    icon: 'none',
                    duration: 1500,
                  })
                  setTimeout(() => {
                    // if (getCurrentPages().length != 0) {
                    //   //刷新当前页面的数据
                    //   getCurrentPages()[getCurrentPages().length - 1].onShow()
                    // }
                    wx.navigateBack({
                      delta:1
                    })
                  }, 2000)
                }
              }
            })
          },
          fail: function() {
            wx.showToast({
              title: '支付失败',
              icon: 'none',
              duration: 1500
            })
          }
        })
      }
    })
  }
})